<?php 
namespace backend\modules\business\methods\base;
error_reporting(0);
use \Yii;
use yii\helpers\Url;
class Basemethod
{
	/**
	 * get参数
	 * @var unknown
	 */
	private $getparam;
	/**
	 * post参数
	 * @var unknown
	 */
	private $postparam;
	/**
	 * 视图组件
	 * @var unknown
	 */
	private $_view;
	/**
	 * 调用方法配置项
	 * @var unknown
	 */
	public $config;
	/**
	 * 实例化类的实例
	 * @var unknown
	 */
	private $Object;
	/**
	 * 分页显示条数
	 * @var unknown
	 */
	public $pageNavLimit;
	/**
	 * 登录用户信息
	 * @var unknown
	 */
	public $userInfo;
	/**
	 * 业务数据过滤
	 * @var unknown
	 */
	public $where;
	/**
	 * 调用的类名
	 * @var unknown
	 */
	public $className;
	/**
	 * ajax请求返回状态信息
	 * @var unknown
	 */
	public $state=1;
	/**
	 * ajax请求返回错误信息
	 * @var unknown
	 */
	public $errorMessage;
	/**
	 * 初始化
	 * @date: 2016-2-27
	 * @author: futao
	 */
	public function __construct($object,$method,$getparam,$postparam,$config)
	{
		$this->Object = $object;
		$this->getparam = $getparam;
		$this->postparam = $postparam;
		$this->userInfo = $this->Object->userInfo;
		$this->pageNavLimit = $this->Object->pageNavLimit;
		$this->where = $this->Object->where;
		$this->config = $config;
		$this->init();
	}
	/**
	 * 返回get参数值
	 * @date: 2016-2-25 
	 * @author: futao
	 */
	public function getParam($key,$default='')
	{
		return $this->getparam[$key] ? $this->getparam[$key] : $default;
	}
	/**
	 * 返回post参数值
	 * @date: 2016-2-25 
	 * @author: futao
	 */
	public function postParam($key,$default=''){
		return $this->postparam[$key] ? $this->postparam[$key] : $default;
	}
	/**
	 * 跳转
	 * @date: 2016-2-25 
	 * @author: futao
	 */
	public function redirect($url, $statusCode = 302)
	{
		return Yii::$app->getResponse()->redirect(Url::to($url), $statusCode);
	}
	/**
	 * 渲染页面
	 * @date: 2016-2-25 
	 * @author: futao
	 */
 	public function render($view, $params = [])
    {
    	$className = strtolower($this->className.'/');
        $content = $this->getView()->render($className.$view, $params, $this->Object);
        return $this->renderContent($content);
    }
    /**
     * 错误提示 
     * @date: 2016-3-22 
     * @author: futao
     */
    public function error($message,$url=-1,$waitTime = 3)
    {
    	$this->Object->error($message,$url=-1,$waitTime = 3);
    }
	/**
	 * 渲染内容
	 * @date: 2016-2-25 
	 * @author: futao
	 */
    public function renderContent($content)
    {
    	$layoutFile = $this->findLayoutFile($this->getView());
    	if ($layoutFile !== false) {
    		return $this->getView()->renderFile($layoutFile, ['content' => $content], $this->Object);
    	} else {
    		return $content;
    	}
    }
    /**
     * 视图组件
     * @date: 2016-2-25 
     * @author: futao
     */
    public function getView()
    {
    	if ($this->_view === null) {
    		$this->_view = Yii::$app->getView();
    	}
    	return $this->_view;
    }
    /**
     * 获取布局文件地址
     * @date: 2016-2-25 
     * @author: futao
     */
    public function findLayoutFile($view)
    {
    	$module = $this->Object->module;
    	if (is_string($this->Object->layout)) {
    		$layout = $this->Object->layout;
    	} elseif ($this->Object->layout === null) {
    		while ($module !== null && $module->layout === null) {
    			$module = $module->module;
    		}
    		if ($module !== null && is_string($module->layout)) {
    			$layout = $module->layout;
    		}
    	}
    
    	if (!isset($layout)) {
    		return false;
    	}
    
    	if (strncmp($layout, '@', 1) === 0) {
    		$file = Yii::getAlias($layout);
    	} elseif (strncmp($layout, '/', 1) === 0) {
    		$file = Yii::$app->getLayoutPath() . DIRECTORY_SEPARATOR . substr($layout, 1);
    	} else {
    		$file = $module->getLayoutPath() . DIRECTORY_SEPARATOR . $layout;
    	}
    
    	if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
    		return $file;
    	}
    	$path = $file . '.' . $view->defaultExtension;
    	if ($view->defaultExtension !== 'php' && !is_file($path)) {
    		$path = $file . '.php';
    	}
    
    	return $path;
    }
    /**
     * 禁用调试栏
     * @date: 2016-2-24
     * @author: futao
     */
    public function disableDebugtool()
    {
    	Yii::$app->session->set('debugState',0);
    }
    /**
     * 设置layout
     * @date: 2016-2-27 
     * @author: futao
     */
    public function setlayout($layout)
    {
    	$this->Object->layout = $layout;
    }
    /**
     * 错误提示
     * @date: 2016-3-2 
     * @author: futao
     */
    public function errorjs($message,$state=0)
    {
		echo "<script>";
		echo "parent.formResult('".$message."','".$state."')";
		echo "</script>";
		exit;
	}
	/**
	 * 判断$_POST数组是否有传入的下标
	 * @date: 2016-3-7 
	 * @author: futao
	 */
	public function variableIsexist($key){
		if(isset($_POST[$key])){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 设置模板action变量
	 * @date: 2016-3-14 
	 * @author: futao
	 */
	public function setAction($action)
	{
		$this->getView()->action = $action;
	}
	/**
	 * 返回json数据
	 * @date: 2016-3-4
	 * @author: futao
	 */
	public function formatData($val='')
	{
		$arr=[
		'state'=>$this->state,
		'message'=>$this->errorMessage,
		'result'=>$val,
		];
		echo json_encode($arr);
		exit;
	}
	/**
	 * 设置错误信息
	 * @date: 2016-3-4
	 * @author: futao
	 */
	public function setError($message)
	{
		$this->state = 0;
		$this->errorMessage = $message;
		$this->formatData();
	}
	/**
	 * 初始化
	 * @date: 2016-3-14 
	 * @author: futao
	 */
    public function init()
    {
    	$classArr = explode("\\", get_class($this));
    	$this->className = $classArr[5];
    	$actionName = $this->config['action'] ? $this->config['action'] : $this->className;
    	$this->setAction(strtolower($actionName));
    }
}